﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Models.LyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class SupplierQuoteService : BaseService, ISupplierQuoteService
    {
        private readonly ISupplierQuoteRepository _supplierQuoteRepository;
        private readonly IUnitOfWork _unitOfWork;
        public SupplierQuoteService(IMapper mapper, ISupplierQuoteRepository supplierQuoteRepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            _supplierQuoteRepository = supplierQuoteRepository;
            _unitOfWork = unitOfWork;
        }

        public bool AddOrModefiySupplierQuote(SupplierQuote model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                _supplierQuoteRepository.UpdEntity(model);
            }
            else
            {
                _supplierQuoteRepository.AddEntity(model);

            }
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelSupplierQuote(long supplierQuoteId)
        {
            var Quote = _supplierQuoteRepository.QueryEntityById(supplierQuoteId);
            if (Quote == null)
            {
                return false;
            }
            _supplierQuoteRepository.DelEntity(Quote);
            return _unitOfWork.SaveChangesEntity();
        }

        public List<SupplierQuote> GetSupplierQuoteData(out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _supplierQuoteRepository.GetEntityAlls();
            
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }

        public SupplierQuote? GetSupplierQuote(long supplierQuoteId)
        {
            return _supplierQuoteRepository.QueryEntityById(supplierQuoteId);
        }
    }
}
